package com.wwwarehouse.usercenter.fragment.register;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CodeEditText;
import com.wwwarehouse.common.http.NetUtils;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.common.UserCenterCommon;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * 填写邀请码
 * Created by xiaoke.ni on 2017/8/11.
 */
public class RCInviteCodeFragment extends RegisterCardBaseFragment implements View.OnClickListener, TextWatcher, CodeEditText.OnInputFinishedListener {
    private View         mRootView;
    private Button       mSureBtn;
    private CodeEditText mCodeEdt;

    private static final int REQUEST_CHECK_INVITE_CODE = 0;
    private RelativeLayout mRlElse;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_rc_invite_code, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initEvent();
    }

    private void initView() {
        mRlElse = findView(mRootView, R.id.rl_else);
        mSureBtn = findView(mRootView, R.id.btn_sure);
        mCodeEdt = findView(mRootView, R.id.et_code);
    }

    private void initEvent() {
        mRlElse.setOnClickListener(this);
        mSureBtn.setOnClickListener(this);
        mCodeEdt.addTextChangedListener(this);
        mCodeEdt.setOnInputFinishedListener(this);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof RCInviteCodeFragment) {
            mActivity.setTitle(R.string.invite_code);
        }
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onClick(View v) {
        if (v == mSureBtn) {
            //验证邀请码
            if (NetUtils.isHttpConnected(mActivity)) {
                checkInviteCode();
            } else {
                toast(mActivity.getResources().getString(R.string.no_net));
            }

        } else if (v == mRlElse) {
            hideSoftKeyBoard(v);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (!TextUtils.isEmpty(mCodeEdt.getText().toString().trim()) && mCodeEdt.getText().toString().trim().length() == 4) {
            mSureBtn.setEnabled(true);
            mSureBtn.setBackgroundResource(R.drawable.common_btn2_solid_selector);
        } else {
            mSureBtn.setEnabled(false);
            mSureBtn.setBackgroundResource(R.drawable.common_btn1_noclick_shape);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    private void checkInviteCode() {
        showProgressDialog(getString(R.string.submiting));
        UserCenterCommon.getInstance().setInviteCode(mCodeEdt.getText().toString().trim());
        Map map = new HashMap();
        map.put("codeType", "0");
        map.put("inviteCode", mCodeEdt.getText().toString().trim());

        NoHttpUtils.httpPost(UserCenterConstant.URL_CHECK_INVITE_CODE, map, mOnResponseListener, REQUEST_CHECK_INVITE_CODE);
    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            if ("0".equals(commonClass.getCode())) {
                //用于返回后更新UI
//                sp.putBooleanValue("isInviteCode",true);
                EventBus.getDefault().post("RCInviteCodeFragment");
                UserCenterCommon.getInstance().setInviteCode(mCodeEdt.getText().toString().trim());
                //验证通过
                popFragment();
            } else {
                DialogTools.getInstance().showCustomWarning(mActivity, "", commonClass.getMsg(), false, null);
//                toast(commonClass.getMsg());
            }
        }

        @Override
        public void onFailed(String result, int what) {
            toast(result);
        }

        @Override
        public void onFinish(int what) {
            dismissProgressDialog();
        }
    };

    @Override
    public void onInputFinished(String s) {
        //隐藏键盘
        hideSoftKeyBoard(mCodeEdt);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hideSoftKeyBoard(mCodeEdt);
    }
}
